package com.beverages.commodities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beverages.commodities.client.SharedFilesClient;
import com.beverages.commodities.common.Constants;
import com.beverages.commodities.common.Result;
import com.beverages.commodities.entity.Commodities;
import com.beverages.commodities.entity.dto.CommoditiesDTO;
import com.beverages.commodities.service.ICommoditiesService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/customer/commodities")
public class CommoditiesController {
    @Resource
    private ICommoditiesService commoditiesService;

    @ApiOperation(
            value = "分页查看商品",
            notes = "必须传参：\n" +
                    "1: pageNum   (Integer)\n" +
                    "2: pageSize   (Integer)\n" +
                    "可选传参：\n" +
                    "1: type   (Integer) -- 默认值：1 ，即查询碳酸饮料类型的奶茶。\n" +
                    "\ttype映射：\n" +
                    "\t\t1: 碳酸饮料\n" +
                    "\t\t2: 奶茶\n" +
                    "\t\t3: 果茶\n" +
                    "\t\t4: 圣代\n" +
                    "2: milkteaName   (String) -- 默认值：\"\" ，即查询该顾客该类型所有奶茶\n",
            response = Result.class,
            tags = {"查看商品"}
    )
    @GetMapping("/page")
    public Result<IPage<CommoditiesDTO>> findPage(@RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize,
                                                  @RequestParam(defaultValue = "1") Integer type,
                                                  @RequestParam(defaultValue = "") String milkteaName) {
        // 创建分页对象
        IPage<Commodities> page = new Page<>(pageNum, pageSize);

        // 创建查询条件
        QueryWrapper<Commodities> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        if (!"".equals(milkteaName)) {
            queryWrapper.like("milktea_name", milkteaName.trim());
        }
        queryWrapper.eq("whether_to_list",1);

        // 执行分页查询
        IPage<Commodities> resultPage = commoditiesService.page(page, queryWrapper);

        // 将查询结果转换为 DTO 列表
        List<CommoditiesDTO> dtoList = resultPage.getRecords().stream().map(commodities -> {
            return commoditiesService.convertCommoditiesEntityToCommoditiesDTO(commodities);
        }).collect(Collectors.toList());

        // 创建新的 IPage 对象来封装 DTO 列表
        IPage<CommoditiesDTO> dtoPage = new Page<>(pageNum, pageSize, resultPage.getTotal());
        dtoPage.setRecords(dtoList);

        // 返回结果
        return Result.success(dtoPage);
    }

    @ApiOperation(
            value = "根据id查询商品，返回封装对象",
            notes = "必须传参：\n" +
                    "milkteaId  (Integer)\n",
            response = Result.class,
            tags = {"根据id查询商品，返回封装对象"}
    )
    @GetMapping("/{milkteaId}")
    public Result<CommoditiesDTO> findOne(@PathVariable Integer milkteaId) {
        return Result.success(commoditiesService.convertCommoditiesEntityToCommoditiesDTO(commoditiesService.getById(milkteaId)));
    }

    @ApiOperation(
            value = "根据id查询商品，返回未封装对象",
            notes = "必须传参：\n" +
                    "commodityId  (Integer)\n",
            response = Result.class,
            tags = {"根据id查询商品，返回未封装对象"}
    )
    @GetMapping("/id/{commodityId}")
    public Result getCommodityById(@PathVariable Integer commodityId) {
        return Result.success(commoditiesService.getById(commodityId));
    }

}
